package com.shop.cereshop.admin.service.product.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.admin.dao.product.CereShopProductDAO;
import com.shop.cereshop.admin.page.product.CanvasProduct;
import com.shop.cereshop.admin.page.product.CanvasShop;
import com.shop.cereshop.admin.param.product.CanvasAdminProductParam;
import com.shop.cereshop.admin.service.product.CereShopProductService;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.utils.EmptyUtils;
import com.shop.cereshop.commons.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CereShopProductServiceImpl implements CereShopProductService {

    @Autowired
    private CereShopProductDAO cereShopProductDAO;

    @Override
    public Page getProducts(CanvasAdminProductParam param) throws CoBusinessException,Exception {
        PageHelper.startPage(param.getPage(),param.getPageSize());
        List<CanvasProduct> list=null;
        if(IntegerEnum.CANVAS_ACTIVITY_GROUP_WORK.getCode().equals(param.getType())){
            list=cereShopProductDAO.getGroupWorkProducts(param);
        }else if(IntegerEnum.CANVAS_ACTIVITY_SECKILL.getCode().equals(param.getType())){
            list=cereShopProductDAO.getSeckillProducts(param);
        }else if(IntegerEnum.CANVAS_ACTIVITY_DISCOUNT.getCode().equals(param.getType())){
            list=cereShopProductDAO.getDiscountProducts(param);
        }else {
            list=cereShopProductDAO.getProducts(param);
        }
        if(!EmptyUtils.isEmpty(list)){
            for (CanvasProduct product : list) {
                if(IntegerEnum.COUPON_STATE_START.getCode().equals(product.getState())){
                    //活动进行中
                    product.setIfEnable(IntegerEnum.NO.getCode());
                    //设置秒杀活动倒计时
                    product.setTime(TimeUtils.getCountDownByTime(TimeUtils.yyMMddHHmmss(),product.getEndTime()));
                }else if(IntegerEnum.COUPON_STATE_READY.getCode().equals(product.getState())){
                    if(IntegerEnum.ENABLE_START.getCode().equals(product.getIfEnable())
                            &&!EmptyUtils.isEmpty(product.getEnableTime())){
                        //如果开启活动预热,计算预热几小时前的时间
                        String enableTime= TimeUtils.headDate(product.getStartTime(),product.getEnableTime());
                        //判断当前时间是否处于预热时间和活动开始时间之间
                        if(TimeUtils.isBelong(enableTime,product.getStartTime())){
                            //如果处于,需要展示预热信息
                            product.setIfEnable(IntegerEnum.YES.getCode());
                        }
                    }
                }
            }
        }
        PageInfo<CanvasProduct> pageInfo=new PageInfo<>(list);
        Page page=new Page(pageInfo.getList(),pageInfo.getTotal());
        return page;
    }

    @Override
    public Page getShops(CanvasAdminProductParam param) throws CoBusinessException {
        PageHelper.startPage(param.getPage(),param.getPageSize());
        List<CanvasShop> list=cereShopProductDAO.getShops(param);
        PageInfo<CanvasShop> pageInfo=new PageInfo<>(list);
        Page page=new Page(pageInfo.getList(),pageInfo.getTotal());
        return page;
    }
}
